package com.bocai.freeman.view.activitys;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.TableRow;

import com.bocai.freeman.R;
import com.bocai.freeman.base.BaseActivity;
import com.bocai.freeman.model.bean.EventMessage;
import com.bocai.freeman.utils.BitmapPhotoUtils;
import com.bocai.freeman.utils.ToolbarHelper;
import com.bocai.freeman.view.adapter.PictureGridAdapter;
import com.bocai.freeman.view.custom.NoScrolGridView;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import me.iwf.photopicker.PhotoPickerActivity;
import me.iwf.photopicker.utils.PhotoPickerIntent;

/**
 * Created by win7 on 2016/5/26.
 */
public class DynamicIssueActivity extends BaseActivity {
    @Bind(R.id.bv_pic_choose)
    NoScrolGridView bvPicChoose;
    @Bind(R.id.tr_permission)
    TableRow trPermission;


    private PictureGridAdapter mAdapter;
    private List<String> pictureList;
    private static int REQUEST = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_dynamic_issue);
        ButterKnife.bind(this);
        EventBus.getDefault().register(this);
        ToolbarHelper.setup(this, getString(R.string.str_issue_dynamic), R.mipmap.left_return, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
        initView();
        initEvent();
    }

    @Override
    protected void initEvent() {
        click(trPermission).subscribe(o -> {
            startActivity(new Intent(this, WhoCanSeeActivity.class));
        });
    }

    @Override
    protected void initView() {
        initPhoto();
    }

    @Subscribe
    public void event(EventMessage eventMessage) {
        if (eventMessage.message.equals("deleteImg")) {
            int position = (Integer) eventMessage.obj;
            pictureList.remove(position);
            mAdapter.upDateList(pictureList);

        }
    }

    private void initPhoto() {
        pictureList = new ArrayList<>();
        mAdapter = new PictureGridAdapter(this, pictureList, 9);
        bvPicChoose.setAdapter(mAdapter);
        bvPicChoose.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (position == pictureList.size()) {
                    PhotoPickerIntent intent = new PhotoPickerIntent(DynamicIssueActivity.this);
                    if (pictureList.size() <= 9) {
                        intent.setPhotoCount(pictureList.size() == 0 ? 9 : 9 - pictureList.size());
                    }
                    intent.setShowCamera(true);
                    intent.setShowGif(true);
                    startActivityForResult(intent, REQUEST);
                }
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST && resultCode == RESULT_OK) {
            if (data != null) {
                ArrayList<String> photos =
                        data.getStringArrayListExtra(PhotoPickerActivity.KEY_SELECTED_PHOTOS);
                for (int i = 0; i < photos.size(); i++) {
                    String indetifyUrl = BitmapPhotoUtils.compress(photos.get(i), this);
//                    Bitmap bitmap = BitmapFactory.decodeFile(indetifyUrl);
                    pictureList.add(indetifyUrl);
//                    photoBase64 += imgToBase64(bitmap) + ",";
//                    if (i == photos.size() - 1) {
//                        photoBase64 = photoBase64.substring(0, photoBase64.length() - 1);
//                    }
                }
                mAdapter.notifyDataSetChanged();
            }

        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
